/*
package com.ruoyi.common.filter;


import com.qiniu.util.Auth;
import com.ruoyi.common.utils.html.EscapeUtil;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.enums.HttpMethod;
import com.ruoyi.common.utils.StringUtils;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

@Component
public class EnhancedXssFilter implements Filter {

    @Value("${OSS.accessKey}")
    private String accessKey;

    @Value("${OSS.secretKey}")
    private String secretKey;

    @Value("${OSS.path}")
    private String qiniuDomain;

    private Auth auth;
    private long expireSeconds = 3600;
    private List<String> excludes = new ArrayList<>();
    private Pattern qiniuPattern;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        String tempExcludes = filterConfig.getInitParameter("excludes");
        if (StringUtils.isNotEmpty(tempExcludes)) {
            String[] urls = tempExcludes.split(",");
            for (String url : urls) {
                excludes.add(url);
            }
        }
        this.auth = Auth.create(accessKey, secretKey);
        // 匹配完整的七牛云URL
        this.qiniuPattern = Pattern.compile(Pattern.quote(qiniuDomain) + "[^\\s\"'<>]+");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;

        if (handleExcludeURL(req)) {
            chain.doFilter(request, response);
            return;
        }

        EnhancedRequestWrapper wrappedRequest = new EnhancedRequestWrapper(req, auth, qiniuDomain, qiniuPattern, expireSeconds);
        chain.doFilter(wrappedRequest, response);
    }

    private boolean handleExcludeURL(HttpServletRequest request) {
        String url = request.getServletPath();
        String method = request.getMethod();
        if (method == null || HttpMethod.DELETE.matches(method)) {
            return true;
        }
        return StringUtils.matches(url, excludes);
    }

    @Override
    public void destroy() {
    }
}

class EnhancedRequestWrapper extends XssHttpServletRequestWrapper {
    private final Auth auth;
    private final String qiniuDomain;
    private final Pattern qiniuPattern;
    private final long expireSeconds;

    public EnhancedRequestWrapper(HttpServletRequest request, Auth auth, String qiniuDomain,
                                  Pattern qiniuPattern, long expireSeconds) throws IOException {
        super(request);
        this.auth = auth;
        this.qiniuDomain = qiniuDomain;
        this.qiniuPattern = qiniuPattern;
        this.expireSeconds = expireSeconds;
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        if (!isJsonRequest()) {
            return super.getInputStream();
        }

        String json = IOUtils.toString(super.getInputStream(), "utf-8");
        if (StringUtils.isEmpty(json)) {
            return super.getInputStream();
        }

        // 处理JSON中的链接
        json = processJsonContent(json);

        // XSS过滤
        json = EscapeUtil.clean(json).trim();

        byte[] jsonBytes = json.getBytes("utf-8");
        final ByteArrayInputStream bis = new ByteArrayInputStream(jsonBytes);

        return new ServletInputStream() {
            @Override
            public boolean isFinished() {
                return true;
            }

            @Override
            public boolean isReady() {
                return true;
            }

            @Override
            public void setReadListener(ReadListener readListener) {
            }

            @Override
            public int read() throws IOException {
                return bis.read();
            }
        };
    }

    private String processJsonContent(String jsonContent) {
        try {
            JSONObject jsonObject = JSON.parseObject(jsonContent);
            processJsonObject(jsonObject);
            return jsonObject.toString();
        } catch (Exception e) {
            // 打印出异常日志，看看是否抛出了错误
            e.printStackTrace();
            return jsonContent;
        }
    }

    private void processJsonObject(JSONObject jsonObject) {
        for (String key : jsonObject.keySet()) {
            Object value = jsonObject.get(key);
            if (value instanceof String) {
                String strValue = (String) value;
                // 处理普通字段中的链接
                if (strValue.startsWith(qiniuDomain)) {
                    // 直接生成私有链接，不需要额外处理URL
                    String privateUrl = auth.privateDownloadUrl(strValue, expireSeconds);
                    jsonObject.put(key, privateUrl);
                }
                // 处理富文本内容
                else if (isRichTextContent(strValue)) {
                    String processedContent = processRichText(strValue);
                    jsonObject.put(key, processedContent);
                }
            } else if (value instanceof JSONObject) {
                processJsonObject((JSONObject) value);
            }
        }
    }

    private boolean isRichTextContent(String content) {
        return content != null && content.contains("<") && content.contains(">") &&
                (content.contains("</p>") || content.contains("</div>") ||
                        content.contains("</span>") || content.contains("<img"));
    }

    private String processRichText(String content) {
        if (content == null) {
            return "";
        }

        StringBuffer result = new StringBuffer();
        Matcher matcher = qiniuPattern.matcher(content);

        while (matcher.find()) {
            String fullUrl = matcher.group();
            // 直接使用完整URL生成私有链接
            String privateUrl = auth.privateDownloadUrl(fullUrl, expireSeconds);
            // 替换特殊字符，防止appendReplacement出错
            matcher.appendReplacement(result, privateUrl.replace("$", "\\$"));
        }
        matcher.appendTail(result);

        return result.toString();
    }
}*/
